package com.netcracker.studportal.server.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.netcracker.studportal.server.model.*;

public class FacadeUtil {
	public static Professor professorParam(String param) throws Exception{
		try {
			JSONParser parser=new JSONParser();	
			Object o=parser.parse(param);
			JSONObject jsonObj = (JSONObject) o;
			Professor prof=new Professor();
			
			if(jsonObj.containsKey("prof_id")) {
				String value=(String) jsonObj.get("prof_id");
				if(!StringUtils.isEmpty(value)){
					prof.setId(Long.parseLong(value));
				}
			}						
			if(jsonObj.containsKey("first_name")) prof.setFirst_name((String) jsonObj.get("first_name"));			
			if(jsonObj.containsKey("last_name")) prof.setLast_name((String) jsonObj.get("last_name"));
						
			if(jsonObj.containsKey("birthday")) {
				String birth=(String) jsonObj.get("birthday");
				if(StringUtils.isEmpty(birth)) prof.setBirthday(null);
				else {
				SimpleDateFormat format=new SimpleDateFormat("dd.MM.yyyy");
				Date date=format.parse(birth);
				prof.setBirthday(date);
				}
			}
			
			if(jsonObj.containsKey("univ_id")) {
				String value=(String) jsonObj.get("univ_id");
				if(!StringUtils.isEmpty(value)){
				University univ=new University();
				univ.setId(Long.parseLong(value));
				prof.setUniversity(univ);
				}
			}
			
			if(jsonObj.containsKey("patronymic")) prof.setPatronymic((String) jsonObj.get("patronymic"));
			
			if(jsonObj.containsKey("note")) prof.setNote((String) jsonObj.get("note"));

			return prof;
							 
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}	
	}
	
	public static User userParam(String param) throws Exception{
		try {
			JSONParser parser=new JSONParser();	
			Object o=parser.parse(param);
			JSONObject jsonObj = (JSONObject) o;
			User user=new User();
			
			if(jsonObj.containsKey("user_id")){
				String value=(String) jsonObj.get("user_id");
				if(!StringUtils.isEmpty(value)){
					user.setId(Long.parseLong(value));
				}
			}						
								
			if(jsonObj.containsKey("type")) user.setType((String) jsonObj.get("type"));
			if(jsonObj.containsKey("login")) user.setLogin((String) jsonObj.get("login"));
			
			return user;
							 
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}	
		
		
	}
	
	public static Exam examParam(String param) throws Exception{
		try {
			JSONParser parser=new JSONParser();	
			Object o=parser.parse(param);
			JSONObject jsonObj = (JSONObject) o;
			Exam exam=new Exam();
			
			if(jsonObj.containsKey("exam_id")) {
				String value=(String) jsonObj.get("exam_id");
				if(!StringUtils.isEmpty(value)){
					exam.setId(Long.parseLong(value));
				}
			}		
			
			
			if(jsonObj.containsKey("subject_id")) {
				String value=(String) jsonObj.get("subject_id");
				if(!StringUtils.isEmpty(value)){
				Subject subj=new Subject();
				subj.setId(Long.parseLong(value));
				exam.setSubject(subj);
				}
			}
			
			if(jsonObj.containsKey("exam_date")) {
				String ex_date=(String) jsonObj.get("exam_date");
				if(StringUtils.isEmpty(ex_date))
			    exam.setExam_date(null);
				else {
				SimpleDateFormat format=new SimpleDateFormat("dd.MM.yyyy hh:mm");
				Date date=format.parse(ex_date);
				exam.setExam_date(date);
			}}
			
			if(jsonObj.containsKey("type")) exam.setType((String) jsonObj.get("type"));
			
			if(jsonObj.containsKey("prof_id")) {
				String value=(String) jsonObj.get("prof_id");
				if(!StringUtils.isEmpty(value)){
				Professor prof=new Professor();
				prof.setId(Long.parseLong(value));
				exam.setProfessor(prof);
				}
			}
			
			return exam;
							 
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}	
		
		
	}
	
	
	public static ExamWithResult examWithResultParam(String param) throws Exception{
		try {
			JSONParser parser=new JSONParser();	
			Object o=parser.parse(param);
			JSONObject jsonObj = (JSONObject) o;
			ExamWithResult exam_res=new ExamWithResult();
			
			if(jsonObj.containsKey("exam_id")) {
				String value=(String) jsonObj.get("exam_id");
				if(!StringUtils.isEmpty(value)){
				exam_res.setExam_id(Long.parseLong(value));
				}
			}
			
			if(jsonObj.containsKey("student_id")) {
				String value=(String) jsonObj.get("student_id");
				if(!StringUtils.isEmpty(value)){
				exam_res.setStudent_id(Long.parseLong(value));
				}
			}
			
			if(jsonObj.containsKey("mark")) {
				String value=(String) jsonObj.get("mark");
				if(!StringUtils.isEmpty(value)){
				 exam_res.setMark(Integer.parseInt(value));
				}
			}
			if(jsonObj.containsKey("note")) exam_res.setNote((String) jsonObj.get("note"));
			
			return exam_res;
							 
		} catch (Exception e) {
			e.printStackTrace();
			
			throw new Exception(e);
		}			
	}
	
	public static Student studentParam(String param) throws Exception{
		try {
			JSONParser parser=new JSONParser();	
			Object o=parser.parse(param);
			JSONObject jsonObj = (JSONObject) o;
			Student stud=new Student();
			
			if(jsonObj.containsKey("student_id")) {
				String value=(String) jsonObj.get("student_id");
				if(!StringUtils.isEmpty(value)){
				stud.setId(Long.parseLong(value));
				}
			}
			if(jsonObj.containsKey("stud_first_name")) stud.setFirst_name((String) jsonObj.get("stud_first_name"));
			if(jsonObj.containsKey("stud_last_name")) stud.setLast_name((String) jsonObj.get("stud_last_name"));
			
			if(jsonObj.containsKey("birthday")) {
				String birthday=(String) jsonObj.get("birthday");
				
				if(StringUtils.isEmpty(birthday)) stud.setBirthday(null);
				else {
				SimpleDateFormat format=new SimpleDateFormat("dd.MM.yyyy");
				Date date=format.parse(birthday);
				stud.setBirthday(date);
			}}
			
			if(jsonObj.containsKey("course")) {
				String value=(String) jsonObj.get("course");
				if(!StringUtils.isEmpty(value)){
					stud.setCourse(Integer.parseInt(value));
				}
			}
			
			if(jsonObj.containsKey("faculty_id")) {
				String value=(String) jsonObj.get("faculty_id");
				if(!StringUtils.isEmpty(value)){
				Faculty faculty=new Faculty();
				faculty.setId(Long.parseLong(value));
				stud.setFaculty(faculty);
				}
			}
			
			if(jsonObj.containsKey("group_num")) {
				String value=(String) jsonObj.get("group_num");
				if(!StringUtils.isEmpty(value)){
					stud.setGroup_num(Integer.parseInt(value));
				}
			}
			
			if(jsonObj.containsKey("note")) stud.setNote((String) jsonObj.get("note"));
			
			return stud;
							 
		} catch (Exception e) {
			e.printStackTrace();
			
			throw new Exception(e);
		}	
	
	}
	
	public static Faculty facultyParam(String param) throws Exception{
		try {
			JSONParser parser=new JSONParser();	
			Object o=parser.parse(param);
			JSONObject jsonObj = (JSONObject) o;
			Faculty f=new Faculty();
			
			if(jsonObj.containsKey("faculty_id")){
				String value=(String) jsonObj.get("faculty_id");
				if(!StringUtils.isEmpty(value)){
				f.setId(Long.parseLong(value));
				}
			}
			
			if(jsonObj.containsKey("faculty_name")) f.setName((String) jsonObj.get("faculty_name"));
			
			if(jsonObj.containsKey("univ_id")) {
				String value=(String) jsonObj.get("univ_id");
				if(!StringUtils.isEmpty(value)){
				University univ=new University();
				univ.setId(Long.parseLong(value));
				f.setUniversity(univ);
				}
			}
					
			return f;
							 
		} catch (Exception e) {
			e.printStackTrace();
			
			throw new Exception(e);
		}			
	}
	
	public static Subject subjectyParam(String param) throws Exception{
		try {
			JSONParser parser=new JSONParser();	
			Object o=parser.parse(param);
			JSONObject jsonObj = (JSONObject) o;
			Subject subj=new Subject();
			
			if(jsonObj.containsKey("subject_id")) {
				String value=(String) jsonObj.get("subject_id");
				if(!StringUtils.isEmpty(value)){
				subj.setId(Long.parseLong(value));
				}
			}
			
			if(jsonObj.containsKey("name")) subj.setName((String) jsonObj.get("name"));
			
			if(jsonObj.containsKey("hours")){
				String value=(String) jsonObj.get("hours");
				if(!StringUtils.isEmpty(value)){
					subj.setHours(Integer.parseInt(value));
				}
			}
			
			if(jsonObj.containsKey("course")){
				String value=(String) jsonObj.get("course");
				if(!StringUtils.isEmpty(value)){
					subj.setCourse(Integer.parseInt(value));
				}
			}
			
			if(jsonObj.containsKey("description")) subj.setDescription((String) jsonObj.get("description"));
			
			return subj;
							 
		} catch (Exception e) {
			e.printStackTrace();
			
			throw new Exception(e);
		}			
	}
	
	public static University universityParam(String param) throws Exception{
		try {
			JSONParser parser=new JSONParser();	
			Object o=parser.parse(param);
			JSONObject jsonObj = (JSONObject) o;
			University univ=new University();
			
			if(jsonObj.containsKey("univ_id")) {
				String value=(String) jsonObj.get("univ_id");
				if(!StringUtils.isEmpty(value)){
				univ.setId(Long.parseLong(value));
				}
			}
			System.out.println("univ "+univ.getId());
			if(jsonObj.containsKey("univ_name")) univ.setName((String) jsonObj.get("univ_name"));
			
			if(jsonObj.containsKey("city")) univ.setCity((String) jsonObj.get("city"));		
			
			return univ;
							 
		} catch (Exception e) {
			e.printStackTrace();
			
			throw new Exception(e);
		}			
	}
	
	public static Material materialParam(String param) throws Exception{
		try {
			JSONParser parser=new JSONParser();	
			Object o=parser.parse(param);
			JSONObject jsonObj = (JSONObject) o;
			Material material=new Material();
			
			if(jsonObj.containsKey("material_id")) {
				String value=(String) jsonObj.get("material_id");
				if(!StringUtils.isEmpty(value)){
				material.setId(Long.parseLong(value));
				}
			}
			
			if(jsonObj.containsKey("exam_id")) {
				String value=(String) jsonObj.get("exam_id");
				if(!StringUtils.isEmpty(value)){
				Exam exam=new Exam();
				exam.setId(Long.parseLong(value));
				material.setExam(exam);
				}
			}
						
			if(jsonObj.containsKey("material_name")) material.setName((String) jsonObj.get("material_name"));
					
			if(jsonObj.containsKey("material_path")) material.setName((String) jsonObj.get("material_path"));
			
			return material;
							 
		} catch (Exception e) {
			e.printStackTrace();
			
			throw new Exception(e);
		}			
	}
	
	/**
	 * Method for extracting input parameters from input JSONObject.
	 * @param jsonStr
	 * 		input params
	 * @param expectedKeys
	 * 		expected keys of parameters in input JSON object
	 * @return
	 * 		map with input parameters: Key = parameter name, Value = parameter value
	 * @throws ParseException, IllegalArgumentException
	 */
	public static Map<String, String> extractInputParams(String jsonStr, String[] expectedKeys) throws IllegalArgumentException, ParseException
	{
		if (StringUtils.isEmpty(jsonStr)) {
			throw new IllegalArgumentException("Argument 'params' cannot be empty!");
		}
		
		JSONParser parser = new JSONParser();
		JSONObject jsonObj = (JSONObject) parser.parse(jsonStr);
		
		Map<String, String> inputParams = new HashMap<String, String>(expectedKeys.length);
		for (String expectedKey : expectedKeys) {
			Object objValue = jsonObj.get(expectedKey);
			if (objValue == null) {
				throw new IllegalArgumentException("Value of parameter = '" + expectedKey + "' cannot be empty!");
			}
			
			String value = objValue.toString();
			if (StringUtils.isEmpty(value)) {
				throw new IllegalArgumentException("Value of parameter = '" + expectedKey + "' cannot be empty!");
			} else {
				inputParams.put(expectedKey, value);
			}
		}
		
		return inputParams;
	}
}
